import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.3
import com.RspInterface 1.0
import "public.js" as Public
import com.ComboData 1.0


//风险客户一览
Item {
    //    id: riskCust
    property string listName: "HisRiskCust"
    property var tabName
    property var branchID
    property bool bChangeWidth: false
    property bool bInital: false
    property string activeCustID: ""
    property ComboData branchModel: g_MainQuo.getMyBranchs()

    property var sectionSpace: 10
    property var kvSpace: 2
    //客户级别下拉
    property ComboData custGradeModel
    signal freshData
    onBranchIDChanged: freshData()
    onFreshData: {
        var rsp = g_MainQuo.queryHisRiskCustList(getCondition())
        var msg = g_MainQuo.getPopMsg()
        if (msg.length > 0) {
            tipInput.text = msg
            tipInput.open()
            return
        }
        frame.tableList.model = rsp.getAlmModel("risk_cust", listName)
    }

    ColumnLayout {
        anchors.fill: parent
        spacing: 10
        RowLayout {
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.right: parent.right
            anchors.rightMargin: 20
            Layout.preferredHeight: 40
            spacing: 5
            z: 30

            Text {
                id: beginT
                text: "开始日期" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.leftMargin: 2
            }
            DatePicker {
                id: b_date

                Layout.preferredWidth: 110
                anchors.left: beginT.right
                anchors.leftMargin: kvSpace
            }

            Text {
                text: qsTr("账户类型") + colon
                anchors.left: b_date.right
                anchors.leftMargin: sectionSpace
            }

            ComboBox {
                id: cmbAcctType
                Layout.preferredWidth: 65
                model: acctTypeModel
            }

            Text {
                text: "代理商" + colon
                anchors.left: cmbAcctType.right
                anchors.leftMargin: sectionSpace
            }
            ComboBox {
                id: cmbBranch
                model: branchModel
                Layout.preferredWidth: 250
                onCurrentIndexChanged: {
                    custGradeModel = null
                    custGradeModel = g_MainQuo.getCustGradeByBranchs(
                                branchModel.get(currentIndex))
                    for (var i = 0; i < custGradeModel.rowCount(); ++i) {
                        console.log("custGradeModel " + custGradeModel.get(i))
                    }
                }
            }
        }
        RowLayout {
            z: 31
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.right: parent.right
            //            anchors.rightMargin: 20
            Layout.preferredHeight: 30
            Layout.alignment: Qt.AlignHCenter
            Text {
                id: endT
                text: "结束日期" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: parent.left
                anchors.leftMargin: 2
            }
            DatePicker {
                id: e_date
                Layout.preferredWidth: 110
                anchors.left: endT.right
                anchors.leftMargin: kvSpace
            }
            Text {
                text: qsTr("风险等级") + colon
            }

            ComboBox {
                id: cmbRiskGrade
                Layout.preferredWidth: 90
                model: ListModel {
                    ListElement {
                        text: " "
                        risk_grade: 01
                    }
                    ListElement {
                        text: "正常"
                        risk_grade: 11
                    }
                    ListElement {
                        text: "追保"
                        risk_grade: 21
                    }
                    ListElement {
                        text: "相对强平"
                        risk_grade: 31
                    }
                    ListElement {
                        text: "绝对强平"
                        risk_grade: 41
                    }
                }
            }

            Text {
                id: custGradeT
                text: "客户级别" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: cmbRiskGrade.right
                anchors.leftMargin: sectionSpace
            }
            ComboBox {
                id: cmbCustGrade
                Layout.alignment: Qt.AlignHCenter
                anchors.left: custGradeT.right
                anchors.leftMargin: kvSpace
                model: custGradeModel
                onCurrentIndexChanged: {
                    console.log("gradeName " + currentText)
                }
            }
            Item {
                Layout.fillWidth: true
            }
            Button {
                id: query
                text: "查询"
                onClicked: {
                    var bDate = b_date.getDateStr()
                    var eDate = e_date.getDateStr()
                    if (parseInt(bDate) > parseInt(eDate)) {
                        tipInput.text = "开始日期不能大于结束日期!"
                        tipInput.open()
                        //                        return
                    } else {
                        //                        getCondition()
                        freshData()
                    }
                }
            }

            //            Button {
            //                text: "关闭"
            //            }
            Item {
                Layout.preferredWidth: 25 //fillWidth把最后一个挤出去的bug补救
            }
        }

        ListFrame {
            id: frame
            Layout.fillHeight: true
            anchors.left: parent.left
            anchors.leftMargin: 5
            anchors.right: parent.right
            anchors.rightMargin: 5
            tablelistName: listName
            onLoadHeaderFinish: bInital = true
            Connections {
                target: colSet
                onUpdateUIColumn: {
                    if (sList == listName) {
                        bInital = false //lqh 更新表头会导致改变列宽，触发保存列宽，没有必要
                        frame.updateColumn(listName)
                        bInital = true
                    }
                }
            }
            onRightClick: {
                rBtnMenu.row = row
                rBtnMenu.col = col
                rBtnMenu.model = frame.tableList.model
                rBtnMenu.popup()
            }
            onGridWidthChange: {
                if (!bInital)
                    return
                console.log("Tabview onListWidthChange " + role + " " + width)
                colCfg.setColumnWidth(listName, tableList.getColumn(col).role,
                                      tableList.getColumn(col).width)
                bChangeWidth = true
            }
        }

        RowLayout {
            Layout.preferredHeight: 40
            spacing: 20
            Button {
                text: "设置列"
                anchors.left: parent.left
                anchors.leftMargin: 25
                onClicked: {
                    colSet.visible = true
                    //                    colSet.refList = frame
                    colSet.loadColumnCfg(listName)
                }
            }
            Item {
                Layout.preferredWidth: parent.width / 4
            }
            Button {
                id: forceSingle
                text: "打印"
                //                enabled: false
                //                anchors.left: parent.left
                //                anchors.leftMargin: parent.width / 3
                onClicked: {


                    //                    var tab = frame.getTab(frame.currentIndex).item
                    //                    console.log("forceSingle " + tab.tableList.model)
                    //                    openSingleForce("手工单客户强平", activeCustID)
                }
            }

            Item {
                Layout.fillWidth: true
            }
            //            Button {
            //                text: "关闭"
            //                anchors.right: parent.right
            //                anchors.rightMargin: 25
            //                onClicked: closeTab(tabName)
            //            }
            //            Item {
            //                Layout.preferredWidth: 25 //fillWidth把最后一个挤出去的bug补救
            //            }
        }
    }
    Button {
        text: "关闭"
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        onClicked: closeTab(tabName)
    }
    function getCondition() {

        var sCond = "#branch_id=" + branchModel.get(cmbBranch.currentIndex)

        if (cmbAcctType.currentIndex !== 0)
            sCond += "#acct_type=" + cmbAcctType.model.get(
                        cmbAcctType.currentIndex).acct_type
        if (cmbRiskGrade.currentIndex !== 0)
            sCond += "#risk_grade_id=" + cmbRiskGrade.model.get(
                        cmbRiskGrade.currentIndex).risk_grade
        if (cmbCustGrade.currentIndex !== 0)
            sCond += "#cust_type=" + custGradeModel.get(
                        cmbCustGrade.currentIndex)
        sCond += "#begin_date=" + b_date.getDateStr()
        sCond += "#end_date=" + e_date.getDateStr()
        console.log("getCondition " + sCond)
        return sCond
    }

    Timer {
        id: timer_save_column
        interval: 50000
        repeat: true
        triggeredOnStart: true
        running: true
        onTriggered: {
            if (!bChangeWidth)
                return
            bChangeWidth = false
            colCfg.saveColumnWidth()
            console.log("save_column ")
        }
    }
}
